const connection = require('../app/database');

class LoginService {
  async verifyLoginName(name, password) {
    const statement = `
      SELECT * FROM admin WHERE name = ? AND password = ?;
    `;
    const [result] = await connection.execute(statement, [name, password]);
    console.log('[login.service]验证用户名是否存在', result);
    return result;
  }

  // 修改密码
  async changePwd(name, oldPwd, newPwd) {
    try {
      const statement = `
        UPDATE admin SET password = ? WHERE name = ? AND password = ?;
      `;
      const [result] = await connection.execute(statement, [newPwd, name, oldPwd]);
      console.log('[login.service_changePwd]{success}', result);
      return true;
    } catch(e) {
      console.log('[login.service_changePwd]{fail}', e);
    }
  }
}

module.exports = new LoginService();